package com.ttl.bc.ar.reqconfirm.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.ap.stkrequest.BCStkRequestBO;
import com.ttl.bc.ap.stkrequest.BCStkRequestForm;
import com.ttl.bc.ar.reqconfirm.db.BCReqConfirmBO;
import com.ttl.bc.ic.stkissue.bean.BCStkIssueBean;
import com.ttl.bc.ic.stkissue.db.BCStkIssueBO;
import com.ttl.common.util.CommonUtils;

public class BCReqConfirmAction extends DispatchAction {
		
	@SuppressWarnings("unchecked")
	public ActionForward findAll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmAction #mode :showAll");
			
		BCReqConfirmBO bo = new BCReqConfirmBO();
		ArrayList results = (ArrayList)bo.findAll(request);
		
		request.getSession().setAttribute("results", results);						
				
		return mapping.findForward("success_findAll");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmAction #mode :edit ");
			
		BCStkRequestBO bo = new BCStkRequestBO();
		BCStkRequestForm issue2form = (BCStkRequestForm)bo.findForm(request);
		
		request.setAttribute("issue2form", issue2form);
						
		return mapping.findForward("blank_edit");
	}	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmAction #mode :update");		
		
		BCStkRequestBO bo = new BCStkRequestBO();
		
		if (bo.update(form, request, response)== true ) {
			return mapping.findForward("success_update");	
		}else {
			return mapping.findForward("fail_update");
		}												
	}	
	
	public ActionForward approveShowCancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		
		BCReqConfirmBO bo = new BCReqConfirmBO();
		BCStkRequestForm issueform = bo.approveShow(request, response);
						
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmAction #mode :approveShowCancel "+
									  "#docNo:"+issueform.getDocNo());			
		
		BCStkIssueBO bo2 = new BCStkIssueBO();
		BCStkIssueBean issue = bo2.find(issueform.getDocNo());
		
//		System.out.println(issue.getDocNo());
//		System.out.println(issue.getDocDate());
//		System.out.println(issue.getCreatorCode());
//		System.out.println(issue.getMyDescription());
		
		//String stkIssueDocNo = CommonUtils.getMaxStkIssueDocNo(issueform.getDocNo());
		request.setAttribute("issue2form", issueform);
		request.setAttribute("inPage", inPage);
		request.setAttribute("issue", issue);
		request.getSession().setAttribute("userName", user);
		System.out.print("##########");
		return mapping.findForward("approveShowCancel");
	}	
	
	public ActionForward approveShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		BCReqConfirmBO bo = new BCReqConfirmBO();
		BCStkRequestForm issueform = bo.approveShow(request, response);
				
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmAction #mode :approveShow "+
									  "#docNo:"+issueform.getDocNo());						
		
		//ต้องใช้ เลขที่เบิกกับขอเบิกอันเดียวกัน เพราะค้นหา เอกสารด้วย hibernate ยาก เพราะมันเป็บเก็บแบบ Set
		String stkIssueDocNo = issueform.getDocNo();//CommonUtils.getMaxStkIssueDocNo(issueform.getDocNo());
		request.setAttribute("issue2form", issueform);
		request.setAttribute("inPage", inPage);
		request.setAttribute("stkIssueDocNo", stkIssueDocNo);
		request.getSession().setAttribute("userName", user);
		
		return mapping.findForward("approveShow");
	}	
	
	public ActionForward approveConfirm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
								
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmAction #mode :approveConfirm "+
									  "#docNo:"+request.getParameter("docNo"));												
		request.setAttribute("inPage", inPage);
		
		BCReqConfirmBO bo = new BCReqConfirmBO();
		
		if (bo.approveConfirm(request, response)==true) {
			return mapping.findForward("success_approve");
		}else{
			return mapping.findForward("fail_approve");
		}
	}
	
	public ActionForward approveConfirmCancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		BCReqConfirmBO bo = new BCReqConfirmBO();
				
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCReqConfirmAction #mode :approveConfirmCancel "+
									  "#docNo:"+request.getParameter("docNo"));		
										
		request.setAttribute("inPage", inPage);
		
		if (bo.approveConfirmCancel(request, response)==true) {
			return mapping.findForward("success_approve_cancel");
		}else{
			return mapping.findForward("fail_approve");
		}
	}	
}
